#include <iostream>
#include <unistd.h>
#include "poll.h"
#include <sys/socket.h>
using namespace std;

int main()
{
    struct pollfd rfds;
    rfds.fd = 0;
    rfds.events = POLLIN;
    rfds.revents = 0;
    while (1)
    {
        int n = poll(&rfds, 1, -1);
        switch (n)
        {
        case -1:
            std::cerr << "poll error" << std::endl;
            break;
        case 0:
            std::cout << "poll timeout" << std::endl;
            break;
        default:
            cout << "有事件发生" << std::endl;

            if (rfds.revents & POLLIN) //判断读事件是否就绪
            {
                std::cout << rfds.fd << " 上面的读事件发生了" << std::endl;
                char buffer[1024];
                ssize_t s = recv(rfds.fd, buffer, sizeof(buffer) - 1, 0);
                if (s > 0)
                {
                    buffer[s] = 0;
                    std::cout << buffer << std::endl;
                }
            }
            break;
        }
    }
    return 0;
}